package com.dmap.analysispoint.service.impl;

import com.dmap.analysispoint.constants.BurialPointConstant;
import com.dmap.analysispoint.dao.*;
import com.dmap.analysispoint.po.*;
import com.dmap.analysispoint.service.GetCompanyIdService;
import com.dmap.analysispoint.service.UncertaintyInfoAnalysisService;
import com.dmap.base.units.DateUtil;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * \* @author:zhouhao
 * \* Date:30/05/2018
 * \* Description:
 * \*
 */
@Service
public class UncertaintyInfoAnalysisServiceImpl implements UncertaintyInfoAnalysisService {

    @Resource
    private BurialPointAddUserMapper burialPointAddUserMapper;
    @Resource
    private BurialPointAddEquipmentMapper burialPointAddEquipmentMapper;
    @Resource
    private BurialPointAddPhoneMapper burialPointAddPhoneMapper;
    @Resource
    private BurialPointHourUserMapper burialPointHourUserMapper;
    @Resource
    private BurialPointHourPhoneMapper burialPointHourPhoneMapper;
    @Resource
    private BurialPointHourEquipmentMapper burialPointHourEquipmentMapper;
    @Resource
    private BurialPointDayUserMapper burialPointDayUserMapper;
    @Resource
    private BurialPointDayPhoneMapper burialPointDayPhoneMapper;
    @Resource
    private BurialPointDayEquipmentMapper burialPointDayEquipmentMapper;
    @Resource
    private BurialPointAppVersionMapper burialPointAppVersionMapper;
    @Resource
    private GetCompanyIdService getCompanyIdService;
    @Resource
    private BurialPointAddAndActivePhoneMapper burialPointAddAndActivePhoneMapper;

    /**
     * @return 处理数据的条数
     * @Description 没有匹配到正确companyId的新增帐号信息再处理
     */
    @Override
    public Integer addUserInfoAgainAnalysis() {
        int pageIndex = 1;
        int pageSize = 1000;
        int success = 0;
        while (pageIndex > 0) {
            BurialPointAddUserExample example = new BurialPointAddUserExample();
            example.createCriteria().andCompanyIdEqualTo(BurialPointConstant.COMPANY_ID);
            example.setLimitStart((pageIndex - 1) * pageSize);
            example.setLimitEnd(pageSize);
            List<BurialPointAddUser> burialPointAddUserList = burialPointAddUserMapper.selectByExample(example);
            if (!CollectionUtils.isEmpty(burialPointAddUserList)) {
                for (BurialPointAddUser burialPointAddUser : burialPointAddUserList) {
                    long companyId = getCompanyIdService.getCompanyIdByProjectId(burialPointAddUser.getProjectId());
                    if (companyId != BurialPointConstant.COMPANY_ID) {
                        //修改该条数据的companyId
                        burialPointAddUser.setCompanyId(companyId);
                        burialPointAddUserMapper.updateByPrimaryKeySelective(burialPointAddUser);
                        success++;
                    }
                }
                pageIndex++;
                burialPointAddUserList.clear();
            } else {
                break;
            }
        }
        return success;
    }

    /**
     * @return 处理数据的条数
     * @Description 没有匹配到正确companyId的新增用户信息再处理
     */
    @Override
    public Integer addPhoneInfoAgainAnalysis() {
        int pageIndex = 1;
        int pageSize = 1000;
        int success = 0;
        while (pageIndex > 0) {
            BurialPointAddPhoneExample example = new BurialPointAddPhoneExample();
            example.createCriteria().andCompanyIdEqualTo(BurialPointConstant.COMPANY_ID);
            example.setLimitStart((pageIndex - 1) * pageSize);
            example.setLimitEnd(pageSize);
            List<BurialPointAddPhone> burialPointAddPhoneList = burialPointAddPhoneMapper.selectByExample(example);
            if (!CollectionUtils.isEmpty(burialPointAddPhoneList)) {
                for (BurialPointAddPhone burialPointAddPhone : burialPointAddPhoneList) {
                    long companyId = getCompanyIdService.getCompanyIdByProjectId(burialPointAddPhone.getProjectId());
                    if (companyId != BurialPointConstant.COMPANY_ID) {
                        //修改该条数据的companyId
                        burialPointAddPhone.setCompanyId(companyId);
                        burialPointAddPhoneMapper.updateByPrimaryKeySelective(burialPointAddPhone);
                        success++;
                    }
                }
                pageIndex++;
                burialPointAddPhoneList.clear();
            } else {
                break;
            }
        }
        return success;
    }

    /**
     * @return 处理数据的条数
     * @Description 没有匹配到正确companyId的新增设备信息再处理
     */
    @Override
    public Integer addEquipmentInfoAgainAnalysis() {
        int pageIndex = 1;
        int pageSize = 1000;
        int success = 0;
        while (pageIndex > 0) {
            BurialPointAddEquipmentExample example = new BurialPointAddEquipmentExample();
            example.createCriteria().andCompanyIdEqualTo(BurialPointConstant.COMPANY_ID);
            example.setLimitStart((pageIndex - 1) * pageSize);
            example.setLimitEnd(pageSize);
            List<BurialPointAddEquipment> burialPointAddEquipmentList = burialPointAddEquipmentMapper.selectByExample(example);
            if (!CollectionUtils.isEmpty(burialPointAddEquipmentList)) {
                for (BurialPointAddEquipment burialPointAddEquipment : burialPointAddEquipmentList) {
                    long companyId = getCompanyIdService.getCompanyIdByProjectId(burialPointAddEquipment.getProjectId());
                    if (companyId != BurialPointConstant.COMPANY_ID) {
                        //修改该条数据的companyId
                        burialPointAddEquipment.setCompanyId(companyId);
                        burialPointAddEquipmentMapper.updateByPrimaryKeySelective(burialPointAddEquipment);
                        success++;
                    }
                }
                pageIndex++;
                burialPointAddEquipmentList.clear();
            } else {
                break;
            }
        }
        return success;
    }

    /**
     * @return 处理数据的条数
     * @Description 没有匹配到正确companyId的小时活跃帐号信息再处理
     */
    @Override
    public Integer activeUserHourInfoAgainAnalysis() {
        int pageIndex = 1;
        int pageSize = 1000;
        int success = 0;
        while (pageIndex > 0) {
            BurialPointHourUserExample example = new BurialPointHourUserExample();
            example.createCriteria().andCompanyIdEqualTo(BurialPointConstant.COMPANY_ID);
            example.setLimitStart((pageIndex - 1) * pageSize);
            example.setLimitEnd(pageSize);
            List<BurialPointHourUser> burialPointHourUserList = burialPointHourUserMapper.selectByExample(example);
            if (!CollectionUtils.isEmpty(burialPointHourUserList)) {
                for (BurialPointHourUser burialPointHourUser : burialPointHourUserList) {
                    long companyId = getCompanyIdService.getCompanyIdByProjectId(burialPointHourUser.getProjectId());
                    if (companyId != BurialPointConstant.COMPANY_ID) {
                        //修改该条数据的companyId
                        burialPointHourUser.setCompanyId(companyId);
                        burialPointHourUserMapper.updateByPrimaryKeySelective(burialPointHourUser);
                    }
                }
                pageIndex++;
                burialPointHourUserList.clear();
            } else {
                break;
            }
        }
        return success;
    }


    /**
     * @return 处理数据的条数
     * @Description 没有匹配到正确companyId的天活跃帐号信息再处理
     */
    @Override
    public Integer activeUserDayInfoAgainAnalysis() {
        int pageIndex = 1;
        int pageSize = 1000;
        int success = 0;
        while (pageIndex > 0) {
            BurialPointDayUserExample example = new BurialPointDayUserExample();
            example.createCriteria().andCompanyIdEqualTo(BurialPointConstant.COMPANY_ID);
            example.setLimitStart((pageIndex - 1) * pageSize);
            example.setLimitEnd(pageSize);
            List<BurialPointDayUser> burialPointDayUserList = burialPointDayUserMapper.selectByExample(example);
            if (!CollectionUtils.isEmpty(burialPointDayUserList)) {
                for (BurialPointDayUser burialPointDayUser : burialPointDayUserList) {
                    long companyId = getCompanyIdService.getCompanyIdByProjectId(burialPointDayUser.getProjectId());
                    if (companyId != BurialPointConstant.COMPANY_ID) {
                        //修改该条数据的companyId
                        burialPointDayUser.setCompanyId(companyId);
                        burialPointDayUserMapper.updateByPrimaryKeySelective(burialPointDayUser);
                    }
                }
                pageIndex++;
                burialPointDayUserList.clear();
            } else {
                break;
            }
        }
        return success;
    }

    /**
     * @return 处理数据的条数
     * @Description 没有匹配到正确companyId的小时活跃手机信息再处理
     */
    @Override
    public Integer activePhoneHourInfoAgainAnalysis() {
        int pageIndex = 1;
        int pageSize = 1000;
        int success = 0;
        while (pageIndex > 0) {
            BurialPointHourPhoneExample example = new BurialPointHourPhoneExample();
            example.createCriteria().andCompanyIdEqualTo(BurialPointConstant.COMPANY_ID);
            example.setLimitStart((pageIndex - 1) * pageSize);
            example.setLimitEnd(pageSize);
            List<BurialPointHourPhone> burialPointHourPhoneList = burialPointHourPhoneMapper.selectByExample(example);
            if (!CollectionUtils.isEmpty(burialPointHourPhoneList)) {
                for (BurialPointHourPhone burialPointHourPhone : burialPointHourPhoneList) {
                    long companyId = getCompanyIdService.getCompanyIdByProjectId(burialPointHourPhone.getProjectId());
                    if (companyId != BurialPointConstant.COMPANY_ID) {
                        //修改该条数据的companyId
                        burialPointHourPhone.setCompanyId(companyId);
                        burialPointHourPhoneMapper.updateByPrimaryKeySelective(burialPointHourPhone);
                    }
                }
                pageIndex++;
                burialPointHourPhoneList.clear();
            } else {
                break;
            }
        }
        return success;
    }

    /**
     * @return 处理数据的条数
     * @Description 没有匹配到正确companyId的天活跃手机信息再处理
     */
    @Override
    public Integer activePhoneDayInfoAgainAnalysis() {
        int pageIndex = 1;
        int pageSize = 1000;
        int success = 0;
        while (pageIndex > 0) {
            BurialPointDayPhoneExample example = new BurialPointDayPhoneExample();
            example.createCriteria().andCompanyIdEqualTo(BurialPointConstant.COMPANY_ID);
            example.setLimitStart((pageIndex - 1) * pageSize);
            example.setLimitEnd(pageSize);
            List<BurialPointDayPhone> burialPointDayPhoneList = burialPointDayPhoneMapper.selectByExample(example);
            if (!CollectionUtils.isEmpty(burialPointDayPhoneList)) {
                for (BurialPointDayPhone burialPointDayPhone : burialPointDayPhoneList) {
                    long companyId = getCompanyIdService.getCompanyIdByProjectId(burialPointDayPhone.getProjectId());
                    if (companyId != BurialPointConstant.COMPANY_ID) {
                        //修改该条数据的companyId
                        burialPointDayPhone.setCompanyId(companyId);
                        burialPointDayPhoneMapper.updateByPrimaryKeySelective(burialPointDayPhone);
                    }
                }
                pageIndex++;
                burialPointDayPhoneList.clear();
            } else {
                break;
            }
        }
        return success;
    }

    /**
     * @return 处理数据的条数
     * @Description 没有匹配到正确companyId的小时活跃设备信息再处理
     */
    @Override
    public Integer activeEquipmentHourInfoAgainAnalysis() {
        int pageIndex = 1;
        int pageSize = 1000;
        int success = 0;
        while (pageIndex > 0) {
            BurialPointHourEquipmentExample example = new BurialPointHourEquipmentExample();
            example.createCriteria().andCompanyIdEqualTo(BurialPointConstant.COMPANY_ID);
            example.setLimitStart((pageIndex - 1) * pageSize);
            example.setLimitEnd(pageSize);
            List<BurialPointHourEquipment> burialPointHourEquipmentList = burialPointHourEquipmentMapper.selectByExample(example);
            if (!CollectionUtils.isEmpty(burialPointHourEquipmentList)) {
                for (BurialPointHourEquipment burialPointHourEquipment : burialPointHourEquipmentList) {
                    long companyId = getCompanyIdService.getCompanyIdByProjectId(burialPointHourEquipment.getProjectId());
                    if (companyId != BurialPointConstant.COMPANY_ID) {
                        //修改该条数据的companyId
                        burialPointHourEquipment.setCompanyId(companyId);
                        burialPointHourEquipmentMapper.updateByPrimaryKeySelective(burialPointHourEquipment);
                    }
                }
                pageIndex++;
                burialPointHourEquipmentList.clear();
            } else {
                break;
            }
        }
        return success;
    }

    /**
     * @return 处理数据的条数
     * @Description 没有匹配到正确companyId的天活跃设备信息再处理
     */
    @Override
    public Integer activeEquipmentDayInfoAgainAnalysis() {
        int pageIndex = 1;
        int pageSize = 1000;
        int success = 0;
        while (pageIndex > 0) {
            BurialPointDayEquipmentExample example = new BurialPointDayEquipmentExample();
            example.createCriteria().andCompanyIdEqualTo(BurialPointConstant.COMPANY_ID);
            example.setLimitStart((pageIndex - 1) * pageSize);
            example.setLimitEnd(pageSize);
            List<BurialPointDayEquipment> burialPointDayEquipmentList = burialPointDayEquipmentMapper.selectByExample(example);
            if (!CollectionUtils.isEmpty(burialPointDayEquipmentList)) {
                for (BurialPointDayEquipment burialPointDayEquipment : burialPointDayEquipmentList) {
                    long companyId = getCompanyIdService.getCompanyIdByProjectId(burialPointDayEquipment.getProjectId());
                    if (companyId != BurialPointConstant.COMPANY_ID) {
                        //修改该条数据的companyId
                        burialPointDayEquipment.setCompanyId(companyId);
                        burialPointDayEquipmentMapper.updateByPrimaryKeySelective(burialPointDayEquipment);
                    }
                }
                pageIndex++;
                burialPointDayEquipmentList.clear();
            } else {
                break;
            }
        }
        return success;
    }

    /**
     * @return 处理数据的条数
     * @Description 没有匹配到正确companyId的app版本信息再处理
     */
    @Override
    public Integer appVersionInfoAgainAnalysis() {
        int pageIndex = 1;
        int pageSize = 1000;
        int success = 0;
        while (pageIndex > 0) {
            BurialPointAppVersionExample example = new BurialPointAppVersionExample();
            example.createCriteria().andCompanyIdEqualTo(BurialPointConstant.COMPANY_ID);
            example.setLimitStart((pageIndex - 1) * pageSize);
            example.setLimitEnd(pageSize);
            List<BurialPointAppVersion> burialPointAppVersionList = burialPointAppVersionMapper.selectByExample(example);
            if (!CollectionUtils.isEmpty(burialPointAppVersionList)) {
                for (BurialPointAppVersion burialPointAppVersion : burialPointAppVersionList) {
                    long companyId = getCompanyIdService.getCompanyIdByProjectId(burialPointAppVersion.getProjectId());
                    if (companyId != BurialPointConstant.COMPANY_ID) {
                        //修改该条数据的companyId
                        burialPointAppVersion.setCompanyId(companyId);
                        burialPointAppVersionMapper.updateByPrimaryKeySelective(burialPointAppVersion);
                        success++;
                    }
                }
                pageIndex++;
                burialPointAppVersionList.clear();
            } else {
                break;
            }
        }
        return success;
    }

    /**
     * @return 处理数据的条数
     * @Description 没有匹配到正确companyId的数据看板预处理信息再处理
     */
    @Override
    public Integer addAndActivePhoneInfoAgainAnalysis() {
        int pageIndex = 1;
        int pageSize = 1000;
        int success = 0;
        while (pageIndex > 0) {
            BurialPointAddAndActivePhoneExample example = new BurialPointAddAndActivePhoneExample();
            example.createCriteria().andCompanyIdEqualTo(BurialPointConstant.COMPANY_ID);
            example.setLimitStart((pageIndex - 1) * pageSize);
            example.setLimitEnd(pageSize);
            List<BurialPointAddAndActivePhone> burialPointAddAndActivePhoneList = burialPointAddAndActivePhoneMapper.selectByExample(example);
            if (!CollectionUtils.isEmpty(burialPointAddAndActivePhoneList)) {
                for (BurialPointAddAndActivePhone burialPointAddAndActivePhone : burialPointAddAndActivePhoneList) {
                    long companyId = getCompanyIdService.getCompanyIdByProjectId(burialPointAddAndActivePhone.getProjectId());
                    if (companyId != BurialPointConstant.COMPANY_ID) {
                        //修改该条数据的companyId
                        burialPointAddAndActivePhone.setCompanyId(companyId);
                        burialPointAddAndActivePhoneMapper.updateByPrimaryKeySelective(burialPointAddAndActivePhone);
                        success++;
                    }
                }
                pageIndex++;
                burialPointAddAndActivePhoneList.clear();
            } else {
                break;
            }
        }
        return success;
    }
}
